package com.genius.adminmanager.user.service.impl;

import java.sql.SQLException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.user.entity.UserJson;
import com.genius.adminmanager.user.service.UserServiceAdapter;
import com.genius.unite_platform.maintain_info.entity.Maintain_infoEntity;
import com.genius.unite_platform.meeting_info_detail.entity.Meeting_info_detailEntity;
import com.genius.workflow.util.ConnectionException;
import com.genius.workflow.util.DBClosedException;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.PasswordMD5;
import com.genius.workflow.util.Pattern;
import com.genius.workflow.util.TimeDiff;

@Service("userEditorService")
public class UserEditorServiceImpl extends UserServiceAdapter{

	@Override
	public JSONObject edit(JSONObject json, JSONObject response) {
		String status = json.containsKey(UserJson.status)?"1":"0";
    	String createTime = TimeDiff.getNowTime(Pattern.yyyyMMddHHmmss);
    	json.put(UserJson.createTime, createTime);
    	json.put(UserJson.status, status);
//    	String pwd = StringUtils.trim(json.getString(UserJson.password));
//    	String username = StringUtils.trim(json.getString(UserJson.username));
//    	json.put(UserJson.password, PasswordMD5.MD5Pwd(username, pwd));
    	
    	// 获取更新的字段
    	String updateField = UserJson.username+
//    			","+UserJson.password+
    			","+UserJson.command+","+UserJson.name
    			+","+UserJson.sex+","+UserJson.phone+","+UserJson.mail+","+UserJson.createTime+","
    			+UserJson.status+","+UserJson.groupId+","+UserJson.jobnumber+","+UserJson.remark;
    	String whereField = UserJson.userId;
    	try {
    		
			DBTools.update(updateField, whereField, "t_user", json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		return response;
	}
	@Override
	public JSONObject update_photo(JSONObject json, JSONObject response) {
    	// 获取更新的字段
    	String updateField ="photo";
    	String whereField = "userId";
    	try {
    		String updateTable = "t_user";
			DBTools2.update(updateField, whereField, updateTable,json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		return response;
		}
	@Override
	public JSONObject reset_password(JSONObject json, JSONObject response) {
		System.out.println("json>"+json);
    	// 获取更新的字段
    	String updateField ="password";
    	String whereField = "userId";
    	try {
    		String updateTable = "t_user";
    		json.put("password", PasswordMD5.MD5Pwd(json.getString("username"), "123456"));
			DBTools2.update(updateField, whereField, updateTable,json);
			response.put(ResponseJson.status, ResponseJson.status_Success);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Success);
		}  catch (ConnectionException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
		} catch (DBClosedException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
    		response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
		} catch (SQLException e) {
			response.put(ResponseJson.status, ResponseJson.status_Failed);
			response.put(ResponseJson.msg, ResponseJson.msg_Edit_Data_Error);
		}
		return response;
		}
}
